/*
 * The Yard Utilties - http://www.theyard.net/
 * 
 * Copyright (c) 2008 by Vlideshow, Inc..  All Rights Resrved.
 * 
 * This library is free software; you can redistribute it and/or modify it under the 
 * terms of the GNU Lesser General Public License as published by the Free Software 
 * Foundation; either version 2.1 of the License, or (at your option) any later 
 * version. 
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along 
 * with this library; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */

/**
 * This class is an example of how to use a net connection state
 * machine.
 */
package net.theyard.components.netconnectionsm
{
  import net.theyard.components.sm.events.StateChangeEvent;
  import net.theyard.components.netconnectionsm.NetConnectionStateMachine;

  /**
   * Create a connection state machine, register a listener for
   * asynchronous events, and then try connecting to a server.
   */
  public class SampleUseCase
  {
    private var ncsm : NetConnectionStateMachine = null;
    public function SampleUseCase()
    {
      ncsm = new NetConnectionStateMachine();
      ncsm.addEventListener(StateChangeEvent.STATE_CHANGE,
          this.onConnectionStateChange);
      ncsm.connect("rtmp://localhost/application");
    }

    /**
     * This method just prints a trace() message whenever the
     * state changes.
     *
     * <p>
     * You can be a lot more sophisticated and switch flex views,
     * update flash buttons, etc.
     * </p>
     * <p>
     * And you never need to worry about why connections didn't occur.
     * </p>
     */
    public function onConnectionStateChange(event:StateChangeEvent):void
    {
      switch(event.getState())
      {
        case NetConnectionStateMachine.STATE_CONNECTING:
          trace("connecting...");
          break;
        case NetConnectionStateMachine.STATE_CONNECTED:
          trace("connected");
          break;
        case NetConnectionStateMachine.STATE_DISCONNECTING:
          trace("disconnecting...");
          break;
        case NetConnectionStateMachine.STATE_DISCONNECTED:
          trace("disconnected");
          break;
        default:
          trace("will never get here...");
          break;
      }
    }
  }
}
